﻿#include "globaldata.h"
#include "myhelper.h"
#include "Console/config.h"
#include "Console/sqlbase.h"
#include "Console/tables.h"
#include "Console/Server/HttpServer.h"
#include "Console/Task/TaskManager.h"

GlobalData::GlobalData(QObject *parent) : QObject(parent)
{
    dataBaseOpen = false;
    serialOpen = false;
}

void GlobalData::Init(){
    config = new Config();
    sqlBase = new SqlBase();
    sqlBase->StopAllAlarm();

    this->InitEquipment();
    this->InitIPData();
    //this->InitAgreement();

    taskManager = TaskManager::GetInstance();
    taskManager->Init();

    connect(this,SIGNAL(Update()),taskManager,SLOT(Init()));

    httpServer = new HttpServer();
    MyHelper::SetMoveToThread(httpServer);

    MyLog::Info("系统初始化完成");
    this->SaveSysLog("系统启动");
}


void GlobalData::InitEquipment(){
    mutex.lock();
    equList.clear();
    QList<Equipment> list = sqlBase->GetEquipmentList();

    for(int i=0; i<list.size(); i++){
        Equipment info = list.at(i);
        //if(!info.enable) continue;

        Equipment *equ = new Equipment();
        equ->devCode = info.devCode;
        equ->devid    = info.devid;
        equ->devname    = info.devname;
        equ->serialName    = info.serialName;

        equ->baudRate    = info.baudRate;
        equ->dataBit   = info.dataBit;
        equ->checkoutBit        = info.checkoutBit;
        equ->stopBit         = info.stopBit;

        equList.append(equ);

        Simulation *sim = new Simulation();
        sim->devid = equ->devid;
        sim->devname = equ->devname;
        simList.append(sim);

        State *str = new State();
        str->devname = equ->devname;
        stateList.append(str);
    }

    mutex.unlock();
}



void GlobalData::UpdateEquipment(){

    QList<Equipment> list = sqlBase->GetEquipmentList();
    for(int i=0; i<list.size(); i++){
        Equipment info = list.at(i);
        Equipment *equ = this->GetEquByCode(info.devCode);
        if(equ){
            equ->devCode  = info.devCode;
            equ->devid    = info.devid;
            equ->devname    = info.devname;
            equ->serialName    = info.serialName;

            equ->baudRate   = info.baudRate;
            equ->dataBit    = info.dataBit;
            equ->checkoutBit        = info.checkoutBit;
            equ->stopBit         = info.stopBit;
        }
    }

}

void GlobalData::InitIPData()
{
    mutex.lock();

    ipList.clear();
    QList<IPData> list = sqlBase->GetIpList();

    for(int i=0; i<list.size(); i++){
        IPData info = list.at(i);
        //if(!info.enable) continue;

        IPData *equ = new IPData();
        equ->Kw    = info.Kw;
        equ->IP    = info.IP;
        equ->Port    = info.Port;


        ipList.append(equ);

    }

    mutex.unlock();
}

void GlobalData::UpdateIPData()
{
    QList<IPData> list = sqlBase->GetIpList();
    for(int i=0; i<list.size(); i++){
        IPData info = list.at(i);
        IPData *equ = this->GetIPByName(info.Kw);
        if(equ){
            equ->Kw    = info.Kw;
            equ->IP    = info.IP;
            equ->Port    = info.Port;
        }
    }
}

void GlobalData::critical(QString Kw)
{
    QString str=Kw+="机天线配置错误";
    MyGUI::ShowMessageBoxError(str);
}

void GlobalData::criTicalSuccess(QString Kw)
{
    QString str=Kw+="机天线配置成功";
    MyGUI::ShowMessageBoxInfo(str);
}


IPData *GlobalData::GetIPByName(QString name)
{
    IPData *sim = nullptr;

    mutex.lock();
    int a=this->ipList.size();
    for(int i=0; i<this->ipList.size(); i++){
        IPData *info = this->ipList[i];
        if(info->Kw == name){
            sim = info;
            break;
        }
    }
    mutex.unlock();

    return sim;
}

int GlobalData::GetEquIndexByCode(QString code){
    mutex.lock();
    int index = -1;
    for(int i=0; i<this->equList.size(); i++){
        Equipment *info = this->equList[i];
        if(info->devCode == code){
            index = i;
            break;
        }
    }
    mutex.unlock();

    return index;
}
Equipment* GlobalData::GetEquByCode(QString code){
    int index = this->GetEquIndexByCode(code);
    if(index >= 0){
        return this->equList[index];
    }else{
        return new Equipment();
    }
}

Simulation* GlobalData::GetSimByCode(QString code){
    int index = this->GetEquIndexByCode(code);
    if(index >= 0){
        return this->simList[index];
    }else{
        return new Simulation();
    }
}
State* GlobalData::GetStateByCode(QString code){
    int index = this->GetEquIndexByCode(code);
    if(index >= 0){
        return this->stateList[index];
    }else{
        return new State();
    }
}

bool GlobalData::CheckUserRole(int role){
    bool b = false;
    if(this->user.login && user.role <= role){
        b = true;
    }
    return true;
}

void GlobalData::SaveSysLog(QString log, int type){
    SysLog syslog;
    syslog.type = type;
    syslog.operater = user.name;
    syslog.describe = log;
    syslog.createTime = QDateTime::currentDateTime();

    this->sqlBase->SaveSysLog(syslog);//执行sql语句 插入
    MyLog::Info(QString("%1 %2 %3").arg(syslog.GetTypeText()).arg(syslog.operater).arg(syslog.describe));
}
